package com.esri.core.tasks.tilecache;

import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.MapGeometry;
import com.esri.core.geometry.MultiPath;
import com.esri.core.geometry.MultiPoint;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.f;
import com.esri.core.internal.util.d;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class ExportTileCacheParameters extends f {
    private static final String AREA_OF_INTEREST_FIELD = "areaOfInterest";
    private static final String CREATE_ITEM_FIELD = "createItem";
    private static final String EXPORT_BY_FIELD = "exportBy";
    private static final String EXPORT_EXTENT_FIELD = "exportExtent";
    private static final String LEVELS_FIELD = "levels";
    private static final String RECOMPRESSION_QUALITY_FIELD = "compressionQuality";
    private static final String RECOMPRESS_FIELD = "optimizeTilesForSize";
    private static final String TILE_PACKAGE_FIELD = "tilePackage";
    private static final long serialVersionUID = 1;
    private Geometry mAreaOfInterest;
    private SpatialReference mAreaOfInterestSR;
    private boolean mCreateAsTilePackage;
    private boolean mCreateItem;
    private ExportBy mExportBy;
    private double[] mLevels;
    private double mMaxRange;
    private double mMinRange;
    private boolean mRecompressTileCache;
    private int mRecompressionQuality;
    private boolean mUsingRange;

    /* loaded from: classes.dex */
    public enum ExportBy {
        ID("levelId");
        
        private String _value;

        ExportBy(String str) {
            this._value = str;
        }

        public String getValue() {
            return this._value;
        }

        public static ExportBy fromString(String str) {
            if ("levelId".equals(str)) {
                return ID;
            }
            return null;
        }

        @Override // java.lang.Enum
        public String toString() {
            return this._value;
        }
    }

    public ExportTileCacheParameters(boolean z, int i, int i2, Geometry geometry, SpatialReference spatialReference) {
        this.mExportBy = ExportBy.ID;
        this.mMinRange = Double.NaN;
        this.mMaxRange = Double.NaN;
        this.mUsingRange = false;
        this.mCreateItem = false;
        this.mCreateAsTilePackage = z;
        this.mMinRange = i;
        this.mMaxRange = i2;
        this.mUsingRange = true;
        this.mExportBy = ExportBy.ID;
        this.mAreaOfInterest = geometry;
        this.mAreaOfInterestSR = spatialReference;
    }

    public ExportTileCacheParameters(boolean z, double[] dArr, ExportBy exportBy, Geometry geometry, SpatialReference spatialReference) {
        this.mExportBy = ExportBy.ID;
        this.mMinRange = Double.NaN;
        this.mMaxRange = Double.NaN;
        this.mUsingRange = false;
        this.mCreateItem = false;
        this.mCreateAsTilePackage = z;
        this.mLevels = dArr;
        this.mExportBy = exportBy;
        this.mAreaOfInterest = geometry;
        this.mAreaOfInterestSR = spatialReference;
    }

    private ExportTileCacheParameters() {
        this.mExportBy = ExportBy.ID;
        this.mMinRange = Double.NaN;
        this.mMaxRange = Double.NaN;
        this.mUsingRange = false;
        this.mCreateItem = false;
    }

    public boolean getRecompressTileCache() {
        return this.mRecompressTileCache;
    }

    public void setRecompressTileCache(boolean z) {
        this.mRecompressTileCache = z;
    }

    public int getRecompressionQuality() {
        return this.mRecompressionQuality;
    }

    public void setRecompressionQuality(int i) {
        if (i < 0 || i > 100) {
            throw new IllegalArgumentException("Recompression quality must be in range 0-100");
        }
        this.mRecompressionQuality = i;
    }

    public int getMinLevelOfDetail() {
        return (int) Math.ceil(this.mMinRange);
    }

    public void setMinLevelOfDetail(int i) {
        this.mExportBy = ExportBy.ID;
        this.mUsingRange = true;
        this.mMinRange = i;
    }

    public int getMaxLevelOfDetail() {
        return (int) Math.ceil(this.mMaxRange);
    }

    public void setMaxLevelOfDetail(int i) {
        this.mExportBy = ExportBy.ID;
        this.mUsingRange = true;
        this.mMaxRange = i;
    }

    public double[] getLevels() {
        return this.mLevels;
    }

    public void setLevels(double[] dArr) {
        this.mUsingRange = false;
        this.mLevels = dArr;
    }

    public boolean isOutputToGeoWarehouse() {
        return this.mCreateItem;
    }

    public void setOutputToGeoWarehouse(boolean z) {
        this.mCreateItem = z;
    }

    @Override // com.esri.core.internal.tasks.f
    public Map<String, String> generateRequestParams() throws Exception {
        HashMap hashMap = new HashMap();
        hashMap.put(TILE_PACKAGE_FIELD, this.mCreateAsTilePackage ? "true" : "false");
        hashMap.put(LEVELS_FIELD, generateLevels());
        hashMap.put(EXPORT_BY_FIELD, this.mExportBy.getValue());
        hashMap.put(RECOMPRESS_FIELD, this.mRecompressTileCache ? "true" : "false");
        hashMap.put(CREATE_ITEM_FIELD, this.mCreateItem ? "true" : "false");
        if (this.mRecompressTileCache) {
            hashMap.put(RECOMPRESSION_QUALITY_FIELD, Integer.toString(this.mRecompressionQuality));
        }
        Geometry geometry = this.mAreaOfInterest;
        if (geometry instanceof Envelope) {
            hashMap.put(EXPORT_EXTENT_FIELD, GeometryEngine.geometryToJson(this.mAreaOfInterestSR, geometry));
        } else if (geometry instanceof Polygon) {
            Envelope envelope = new Envelope();
            this.mAreaOfInterest.queryEnvelope(envelope);
            hashMap.put(EXPORT_EXTENT_FIELD, GeometryEngine.geometryToJson(this.mAreaOfInterestSR, envelope));
            SpatialReference create = SpatialReference.create(4326);
            hashMap.put(AREA_OF_INTEREST_FIELD, String.format("{ \"features\": [{\"geometry\":%s}]}", GeometryEngine.geometryToJson(create, (Polygon) GeometryEngine.project(this.mAreaOfInterest, this.mAreaOfInterestSR, create))));
        }
        return hashMap;
    }

    @Override // com.esri.core.internal.tasks.f
    public boolean validate() {
        return Geometry.isArea(this.mAreaOfInterest.getType().value());
    }

    public static ExportTileCacheParameters fromJson(JsonParser jsonParser) throws Exception {
        if (d.c(jsonParser)) {
            ExportTileCacheParameters exportTileCacheParameters = new ExportTileCacheParameters();
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if (currentName.equals(TILE_PACKAGE_FIELD)) {
                    exportTileCacheParameters.mCreateAsTilePackage = jsonParser.getBooleanValue();
                } else if (currentName.equals(LEVELS_FIELD)) {
                    exportTileCacheParameters.getLevelsFromJSON(jsonParser.getText());
                } else if (currentName.equals(EXPORT_BY_FIELD)) {
                    exportTileCacheParameters.mExportBy = ExportBy.fromString(jsonParser.getText());
                } else if (currentName.equals(AREA_OF_INTEREST_FIELD)) {
                    MapGeometry jsonToGeometry = GeometryEngine.jsonToGeometry(d.c(jsonParser.getText()));
                    if (jsonToGeometry != null) {
                        exportTileCacheParameters.mAreaOfInterest = jsonToGeometry.getGeometry();
                        exportTileCacheParameters.mAreaOfInterestSR = jsonToGeometry.getSpatialReference();
                    }
                } else if (currentName.equals(RECOMPRESS_FIELD)) {
                    exportTileCacheParameters.mRecompressTileCache = jsonParser.getBooleanValue();
                } else if (currentName.equals(RECOMPRESSION_QUALITY_FIELD)) {
                    exportTileCacheParameters.mRecompressionQuality = jsonParser.getIntValue();
                } else if (currentName.equals(CREATE_ITEM_FIELD)) {
                    exportTileCacheParameters.mCreateItem = jsonParser.getBooleanValue();
                } else {
                    jsonParser.skipChildren();
                }
            }
            return exportTileCacheParameters;
        }
        return null;
    }

    public String toJson() throws IOException {
        StringWriter stringWriter = new StringWriter();
        JsonGenerator a = d.a(stringWriter);
        a.writeStartObject();
        a.writeBooleanField(TILE_PACKAGE_FIELD, this.mCreateAsTilePackage);
        a.writeStringField(LEVELS_FIELD, generateLevels());
        a.writeStringField(EXPORT_BY_FIELD, this.mExportBy.getValue());
        a.writeStringField(AREA_OF_INTEREST_FIELD, GeometryEngine.geometryToJson(this.mAreaOfInterestSR, this.mAreaOfInterest));
        a.writeBooleanField(RECOMPRESS_FIELD, this.mRecompressTileCache);
        a.writeNumberField(RECOMPRESSION_QUALITY_FIELD, this.mRecompressionQuality);
        a.writeBooleanField(CREATE_ITEM_FIELD, this.mCreateItem);
        a.writeEndObject();
        a.close();
        return stringWriter.toString();
    }

    public int hashCode() {
        Geometry geometry = this.mAreaOfInterest;
        int generateGeometryHashcode = ((geometry == null ? 0 : generateGeometryHashcode(geometry)) + 31) * 31;
        SpatialReference spatialReference = this.mAreaOfInterestSR;
        int id = (((generateGeometryHashcode + (spatialReference == null ? 0 : spatialReference.getID())) * 31) + (this.mCreateAsTilePackage ? 1231 : 1237)) * 31;
        double[] dArr = this.mLevels;
        int hashCode = (id + (dArr == null ? 0 : Arrays.hashCode(dArr))) * 31;
        ExportBy exportBy = this.mExportBy;
        return ((((((((((hashCode + (exportBy != null ? exportBy.hashCode() : 0)) * 31) + (this.mRecompressTileCache ? 1231 : 1237)) * 31) + this.mRecompressionQuality) * 31) + Double.valueOf(this.mMinRange).hashCode()) * 31) + Double.valueOf(this.mMaxRange).hashCode()) * 31) + (this.mCreateItem ? 1231 : 1237);
    }

    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj != null && getClass() == obj.getClass()) {
            ExportTileCacheParameters exportTileCacheParameters = (ExportTileCacheParameters) obj;
            Geometry geometry = this.mAreaOfInterest;
            if (geometry == null) {
                if (exportTileCacheParameters.mAreaOfInterest != null) {
                    return false;
                }
            } else if (!GeometryEngine.equals(geometry, exportTileCacheParameters.mAreaOfInterest, this.mAreaOfInterestSR)) {
                return false;
            }
            SpatialReference spatialReference = this.mAreaOfInterestSR;
            if (spatialReference == null) {
                if (exportTileCacheParameters.mAreaOfInterestSR != null) {
                    return false;
                }
            } else if (spatialReference.getID() != exportTileCacheParameters.mAreaOfInterestSR.getID()) {
                return false;
            }
            if (this.mCreateAsTilePackage != exportTileCacheParameters.mCreateAsTilePackage) {
                return false;
            }
            double[] dArr = this.mLevels;
            if (dArr == null) {
                if (exportTileCacheParameters.mLevels != null) {
                    return false;
                }
            } else if (!Arrays.equals(dArr, exportTileCacheParameters.mLevels)) {
                return false;
            }
            ExportBy exportBy = this.mExportBy;
            if (exportBy == null) {
                if (exportTileCacheParameters.mExportBy != null) {
                    return false;
                }
            } else if (!exportBy.equals(exportTileCacheParameters.mExportBy)) {
                return false;
            }
            return this.mRecompressTileCache == exportTileCacheParameters.mRecompressTileCache && this.mRecompressionQuality == exportTileCacheParameters.mRecompressionQuality && this.mMinRange == exportTileCacheParameters.mMinRange && this.mMaxRange == exportTileCacheParameters.mMaxRange && this.mCreateItem == exportTileCacheParameters.mCreateItem;
        }
        return false;
    }

    private String generateLevels() {
        int i = 0;
        if (this.mUsingRange) {
            return String.format("%d-%d", Integer.valueOf((int) Math.ceil(this.mMinRange)), Integer.valueOf((int) Math.ceil(this.mMaxRange)));
        }
        StringBuilder sb = new StringBuilder();
        double[] dArr = this.mLevels;
        int length = dArr.length;
        int i2 = 0;
        while (i < length) {
            double d = dArr[i];
            int i3 = i2 + 1;
            sb.append(i2 > 0 ? "," : "");
            sb.append(this.mExportBy == ExportBy.ID ? Integer.valueOf((int) d).toString() : Double.valueOf(d).toString());
            i++;
            i2 = i3;
        }
        return sb.toString();
    }

    private void getLevelsFromJSON(String str) {
        if (str.contains("-")) {
            String[] split = str.split("-");
            if (split.length != 2) {
                throw new IllegalArgumentException("Currently only support a single range for tile cache levels");
            }
            this.mUsingRange = true;
            this.mMinRange = Double.valueOf(split[0]).doubleValue();
            this.mMaxRange = Double.valueOf(split[1]).doubleValue();
            return;
        }
        String[] split2 = str.split(",");
        this.mLevels = new double[split2.length];
        for (int i = 0; i < split2.length; i++) {
            this.mLevels[i] = Double.valueOf(split2[i]).doubleValue();
        }
    }

    private int generateGeometryHashcode(Geometry geometry) {
        int i = 1;
        if (geometry != null) {
            int i2 = 0;
            if (geometry instanceof MultiPath) {
                MultiPath multiPath = (MultiPath) geometry;
                while (i2 < multiPath.getPointCount()) {
                    i = generatePointHashCode(31, i, multiPath.getPoint(i2));
                    i2++;
                }
                return i;
            } else if (geometry instanceof MultiPoint) {
                MultiPoint multiPoint = (MultiPoint) geometry;
                while (i2 < multiPoint.getPointCount()) {
                    i = generatePointHashCode(31, i, multiPoint.getPoint(i2));
                    i2++;
                }
                return i;
            } else if (geometry instanceof Envelope) {
                Envelope envelope = (Envelope) geometry;
                return generatePointHashCode(31, generatePointHashCode(31, generatePointHashCode(31, generatePointHashCode(31, 1, envelope.getUpperLeft()), envelope.getUpperRight()), envelope.getLowerLeft()), envelope.getLowerRight());
            } else {
                return generatePointHashCode(31, 1, (Point) geometry);
            }
        }
        return 1;
    }

    private int generatePointHashCode(int i, int i2, Point point) {
        return (i * i2) + Double.valueOf(point.getX()).hashCode() + Double.valueOf(point.getY()).hashCode();
    }
}
